//
//  CartGoodsCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/28.
//  Copyright © 2021 冯传昌. All rights reserved.
//


#define CellWidth (ScreenWidth - 20)

#import "CartGoodsCell.h"

#import "GoodsDetailsViewController.h"

@interface CartGoodsCell()<UITextFieldDelegate,UIGestureRecognizerDelegate>

@property (nonatomic,strong) UIButton *selectBtn;

@property (nonatomic,strong) UIImageView *goodsImgV;//商品图片

@property (nonatomic,strong) UILabel *goodsInfoLB;//商品介绍lb

@property (nonatomic,strong) UILabel *goodsPriceLB;//商品价格lb

@property (nonatomic,strong) UITextField *goodsNumber_TF;//商品数量

@end

@implementation CartGoodsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}
#pragma mark - setter
- (void)setModel:(CartGoodsModel *)model{
    _model = model;
    
    [self.goodsImgV sd_setImageWithURL:[NSURL URLWithString:_model.picture]];
    self.goodsInfoLB.text = _model.name;
    self.goodsPriceLB.text = [Configuration getTotalPriceWithPrice:_model.price withTbPrice:_model.tbprice];
    self.goodsNumber_TF.text = [NSString stringWithFormat:@"%ld",(long)_model.number];
    [self.selectBtn setImage:_model.isSelect == YES ? LoadImage(@"购物车_选中"):LoadImage(@"购物车_未选中") forState:normal];
}

#pragma mark - UI
-(void)initUI{
    [self.contentView addSubview:self.selectBtn];
    [self.contentView addSubview:self.goodsImgV];
    [self.contentView addSubview:self.goodsInfoLB];
    [self.contentView addSubview:self.goodsPriceLB];
    [self.contentView addSubview:self.goodsNumber_TF];
}

#pragma mark - lazy
- (UIButton *)selectBtn{
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.frame = CGRectMake(14, 15+32, 16, 16);
        [_selectBtn setImage:LoadImage(@"购物车_未选中") forState:normal];
        [_selectBtn addTarget:self action:@selector(selectGoodClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}
//商品图片
-(UIImageView *)goodsImgV{
    if (!_goodsImgV) {
        _goodsImgV = [[UIImageView alloc] initWithFrame:CGRectMake(self.selectBtn.right + 12, 15, 80, 80)];
        _goodsImgV.backgroundColor = MainBackColor;
        _goodsImgV.clipsToBounds = YES;
        _goodsImgV.layer.cornerRadius = 4;
        
        _goodsImgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        tap.delegate = self;
        [_goodsImgV addGestureRecognizer:tap];
    }
    return _goodsImgV;
}
//商品信息简介lb
-(UILabel *)goodsInfoLB{
    if (!_goodsInfoLB) {
        _goodsInfoLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImgV.right+10, self.goodsImgV.top, CellWidth - 156, 40)];
        _goodsInfoLB.numberOfLines = 2;
        _goodsInfoLB.textAlignment = NSTextAlignmentLeft;
        _goodsInfoLB.textColor = MainBlackTextColor;
        _goodsInfoLB.font = FontSet(14);
    }
    return _goodsInfoLB;
}
//商品价格lb
-(UILabel *)goodsPriceLB{
    if (!_goodsPriceLB) {
        _goodsPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImgV.right+10, self.goodsImgV.bottom - 20, CellWidth - 156 - 82, 18)];
        _goodsPriceLB.textAlignment = NSTextAlignmentLeft;
        _goodsPriceLB.textColor = RGB(255, 34, 21);
        _goodsPriceLB.font = FontSet(13);
    }
    return _goodsPriceLB;
}

- (UITextField *)goodsNumber_TF{
    if (!_goodsNumber_TF) {
        _goodsNumber_TF = [[UITextField alloc] initWithFrame:CGRectMake(CellWidth - 82 - 14, self.goodsImgV.bottom - 27, 80, 25)];
        _goodsNumber_TF.delegate = self;
        _goodsNumber_TF.font = FontSet(13);
        _goodsNumber_TF.textAlignment = NSTextAlignmentCenter;
        _goodsNumber_TF.textColor = MainBlackTextColor;
        _goodsNumber_TF.clipsToBounds = YES;
        _goodsNumber_TF.layer.cornerRadius = 2;
        _goodsNumber_TF.layer.borderColor = MainPureColor(240).CGColor;
        _goodsNumber_TF.layer.borderWidth = 1;
        
        _goodsNumber_TF.leftView = [self leftView];
        _goodsNumber_TF.leftViewMode = UITextFieldViewModeAlways;
        _goodsNumber_TF.rightView = [self rightView];
        _goodsNumber_TF.rightViewMode = UITextFieldViewModeAlways;
    }
    return _goodsNumber_TF;
}

#pragma mark - SEL
// 选中/未选中  商品
-(void)selectGoodClick{
    self.model.isSelect = !self.model.isSelect;
    [self.selectBtn setImage:self.model.isSelect == YES ? LoadImage(@"购物车_未选中"):LoadImage(@"购物车_未选中") forState:normal];
    
    if (self.updateModelStatusBlock) {
        self.updateModelStatusBlock(self.model);
    }
}
-(void)goodsLessClick:(UIGestureRecognizer *)ges{
    if (self.model.number <= 1) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除这个商品吗？" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self deleteGoods];
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:confirmAction];
        [alert addAction:cancelAction];
        [[Configuration getCurrentVC] presentViewController:alert animated:YES completion:nil];
        return;
    }
    WEAKSELF(self);
    //商品减少
    [NetWorkingCart cartGoosLess:self.model.ID skuId:self.model.skuId withSuccessBlock:^(id  _Nonnull responseObj) {
        self.model.number = self.model.number - 1;
        self.goodsNumber_TF.text = [NSString stringWithFormat:@"%ld",(long)self.model.number];
        
        if (weakSelf.updateModelStatusBlock) {
            weakSelf.updateModelStatusBlock(self.model);
        }
    }];
}
-(void)goodsAddClick:(UIGestureRecognizer *)ges{
    if (self.model.stock == 0) {
        [[XNProgressHUD shared] showWithTitle:@"商品库存数量没有那么多了!"];
        return;
    }
    WEAKSELF(self);
    //商品增加
    [NetWorkingCart cartGoosAdd:self.model.ID skuId:self.model.skuId withSuccessBlock:^(id  _Nonnull responseObj) {
        self.model.number = self.model.number + 1;
        self.goodsNumber_TF.text = [NSString stringWithFormat:@"%ld",(long)self.model.number];
        
        if (weakSelf.updateModelStatusBlock) {
            weakSelf.updateModelStatusBlock(self.model);
        }
    }];
}

//点击头像进入商品详情
-(void)tapClick:(UIGestureRecognizer *)ges{
    [self jumpGoodsDetailWithID:self.model.productId];
}
-(void)jumpGoodsDetailWithID:(NSInteger)goodsId{
    GoodsDetailsViewController *vc = [[GoodsDetailsViewController alloc] init];
    vc.goodsID = goodsId;
    [[Configuration getCurrentVC].navigationController pushViewController:vc animated:YES];
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}

#pragma mark - TF    leftView  rightView
-(UIView *)leftView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(5, 12, 15, 1)];
    lineV.backgroundColor = MainGrayTextColor;
    [view addSubview:lineV];
    
    UIView *lineV2 = [[UIView alloc] initWithFrame:CGRectMake(24, 0, 1, 25)];
    lineV2.backgroundColor = MainPureColor(240);
    [view addSubview:lineV2];
    
    view.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goodsLessClick:)];
    tap.delegate = self;
    [view addGestureRecognizer:tap];
    
    return view;
}
-(UIView *)rightView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(5, 12, 15, 1)];
    lineV.backgroundColor = MainGrayTextColor;
    [view addSubview:lineV];
    
    UIView *lineV2 = [[UIView alloc] initWithFrame:CGRectMake(12, 5, 1, 15)];
    lineV2.backgroundColor = MainGrayTextColor;
    [view addSubview:lineV2];
    
    UIView *lineV3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 25)];
    lineV3.backgroundColor = MainPureColor(240);
    [view addSubview:lineV3];
    
    view.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goodsAddClick:)];
    tap.delegate = self;
    [view addGestureRecognizer:tap];
    
    return view;
}

//删除商品
-(void)deleteGoods{
    NSArray *arr = @[[NSString stringWithFormat:@"%ld",self.model.ID]];
    [NetWorkingCart deleteCartListWithIds:arr withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] showWithTitle:@"删除成功"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshShoppingCartNoti" object:nil];
    }];
}


@end
